// 文件上传配置接口
export interface FileUploadConfig {
  accept?: string; // 接受的文件类型，如 "image" 或 "all"
  maxSize?: number; // 最大文件大小（MB）
  maxCount?: number; // 最大文件数量
  uploadUrl?: string; // 上传接口地址
  listType?: "text" | "picture" | "picture-card"; // 显示类型
  tips?: string; // 上传提示信息
}

// 选项数据接口
export interface OptionItem {
  value: string;
  text: string;
}

// 表单字段配置接口
export interface FieldConfig {
  key: string;
  label: string;
  type: "input" | "textarea" | "picker" | "datetime" | "image-upload" | "file-upload" | "readonly";
  placeholder: string;
  required?: boolean;
  inputType?: string;
  maxlength?: number;
  pattern?: string; // 正则表达式验证
  patternErrorMessage?: string; // 正则验证失败的错误信息
  options?: string[];
  optionsData?: OptionItem[]; // 选项数据
  fileConfig?: {
    isIdCard?: boolean;
    idCardType?: "front" | "back";
  }; // 文件上传配置，与XenDynamicDetailsCard组件兼容
  readonly?: boolean; // 只读字段标识
  showInCreate?: boolean; // 是否在新增页面显示
  showInEdit?: boolean; // 是否在编辑页面显示
  showInDetails?: boolean; // 是否在详情页面显示
  validation?: (
    value: any,
    formData?: any,
  ) => { valid: boolean; message: string } | true;
}

// 子表字段配置接口
export interface DetailFieldConfig {
  key: string;
  label: string;
  formatter?: (value: any, item?: any) => string;
}

// 表单分组接口
export interface FormSection {
  key: string;
  title: string;
  fields: FieldConfig[];
  isDetailList?: boolean; // 标识是否为明细列表分组
}

// 招聘需求申请表单配置
export const recruitRequestFormConfig: Record<string, FieldConfig> = {
  // 审批信息
  approver: {
    key: "approver",
    label: "审批人",
    type: "readonly",
    placeholder: "审批人",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  approvalTime: {
    key: "approvalTime",
    label: "审批时间",
    type: "readonly",
    placeholder: "审批时间",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  approvalOpinion: {
    key: "approvalOpinion",
    label: "审批意见",
    type: "readonly",
    placeholder: "审批意见",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },

  // 单据信息
  billCode: {
    key: "billCode",
    label: "单据编号",
    type: "readonly",
    placeholder: "系统自动生成",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  billStatus: {
    key: "billStatus",
    label: "单据状态",
    type: "readonly",
    placeholder: "单据状态",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  workflowStatus: {
    key: "workflowStatus",
    label: "状态",
    type: "readonly",
    placeholder: "状态",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  remark: {
    key: "remark",
    label: "备注",
    type: "readonly",
    placeholder: "备注信息",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },

  // 工作信息
  company: {
    key: "company",
    label: "公司",
    type: "readonly",
    placeholder: "公司",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  group: {
    key: "group",
    label: "所属部门",
    type: "readonly",
    placeholder: "所属部门",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
};

// 子表字段配置 - 招聘需求明细
export const recruitRequestDetailFields: DetailFieldConfig[] = [
  { key: "BillNo", label: "用人需求池单号" },
  { key: "BillCode", label: "用人需求单号" },
  { key: "OrgName", label: "用人需求公司" },
  { key: "GroupName", label: "用人需求部门" },
  { key: "JobPositionName", label: "用人需求岗位" },
  { key: "RecruitRequestBillName", label: "招聘申请单号" },
  { key: "RecruitCount", label: "招聘人数" },
];

// 表单分组配置
export const formSections: FormSection[] = [
  {
    key: "billInfo",
    title: "单据信息",
    fields: [
      recruitRequestFormConfig.billCode,
      recruitRequestFormConfig.billStatus,
      recruitRequestFormConfig.workflowStatus,
      recruitRequestFormConfig.remark,
    ],
  },
  {
    key: "approvalInfo",
    title: "审批信息",
    fields: [
      recruitRequestFormConfig.approver,
      recruitRequestFormConfig.approvalTime,
      recruitRequestFormConfig.approvalOpinion,
    ],
  },
  {
    key: "workInfo",
    title: "工作信息",
    fields: [
      recruitRequestFormConfig.company,
      recruitRequestFormConfig.group,
    ],
  },
  {
    key: "detailInfo",
    title: "招聘需求明细",
    fields: [], // 明细列表将通过特殊组件展示
    isDetailList: true, // 标识这是一个明细列表分组
  },
];

// 根据页面类型过滤表单分组
export const getFormSectionsByPageType = (pageType: 'create' | 'edit' | 'details'): FormSection[] => {
  return formSections.map(section => ({
    ...section,
    fields: section.fields.filter(field => {
      if (pageType === 'create') {
        return field.showInCreate !== false;
      } else if (pageType === 'edit') {
        return field.showInEdit !== false;
      } else if (pageType === 'details') {
        // 详情页根据showInDetails配置显示字段
        return field.showInDetails !== false;
      }
      return false;
    })
  })).filter(section => section.fields.length > 0);
};

// 表单验证函数
export const validateForm = (
  formData: Record<string, any>,
): { valid: boolean; errors: Record<string, string> } => {
  const errors: Record<string, string> = {};
  let valid = true;

  // 遍历所有字段配置
  Object.values(recruitRequestFormConfig).forEach((field) => {
    const value = formData[field.key];

    // 必填字段验证
    if (field.required && !value) {
      errors[field.key] = `请输入${field.label}`;
      valid = false;
      return;
    }

    // 正则表达式验证
    if (field.pattern && value) {
      const pattern = new RegExp(field.pattern);
      if (!pattern.test(value)) {
        errors[field.key] =
          field.patternErrorMessage || `${field.label}格式不正确`;
        valid = false;
        return;
      }
    }

    // 自定义验证
    if (field.validation && value !== undefined) {
      const result = field.validation(value, formData);
      if (result !== true) {
        errors[field.key] = result.message;
        valid = false;
        return;
      }
    }
  });

  return { valid, errors };
};
